﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicViet.Models;

namespace MedicViet.Controllers
{ 
    public class RecordIndicatorController : Controller
    {
        private MedicVietEntities db = new MedicVietEntities();

        //
        // GET: /RecordIndicator/

        public ViewResult Index()
        {
            var recordindicators = db.RecordIndicators.Include("Indicator").Include("Record");
            return View(recordindicators.ToList());
        }

        //
        // GET: /RecordIndicator/Details/5

        public ViewResult Details(int id)
        {
            RecordIndicator recordindicator = db.RecordIndicators.Single(r => r.indicatorId == id);
            return View(recordindicator);
        }

        //
        // GET: /RecordIndicator/Create

        public ActionResult Create()
        {
            ViewBag.indicatorId = new SelectList(db.Indicators, "id", "name");
            ViewBag.recordId = new SelectList(db.Records, "id", "description");
            return View();
        } 

        //
        // POST: /RecordIndicator/Create

        [HttpPost]
        public ActionResult Create(RecordIndicator recordindicator)
        {
            if (ModelState.IsValid)
            {
                db.RecordIndicators.AddObject(recordindicator);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.indicatorId = new SelectList(db.Indicators, "id", "name", recordindicator.indicatorId);
            ViewBag.recordId = new SelectList(db.Records, "id", "description", recordindicator.recordId);
            return View(recordindicator);
        }
        
        //
        // GET: /RecordIndicator/Edit/5
 
        public ActionResult Edit(int id)
        {
            RecordIndicator recordindicator = db.RecordIndicators.Single(r => r.indicatorId == id);
            ViewBag.indicatorId = new SelectList(db.Indicators, "id", "name", recordindicator.indicatorId);
            ViewBag.recordId = new SelectList(db.Records, "id", "description", recordindicator.recordId);
            return View(recordindicator);
        }

        //
        // POST: /RecordIndicator/Edit/5

        [HttpPost]
        public ActionResult Edit(RecordIndicator recordindicator)
        {
            if (ModelState.IsValid)
            {
                db.RecordIndicators.Attach(recordindicator);
                db.ObjectStateManager.ChangeObjectState(recordindicator, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.indicatorId = new SelectList(db.Indicators, "id", "name", recordindicator.indicatorId);
            ViewBag.recordId = new SelectList(db.Records, "id", "description", recordindicator.recordId);
            return View(recordindicator);
        }

        //
        // GET: /RecordIndicator/Delete/5
 
        public ActionResult Delete(int id)
        {
            RecordIndicator recordindicator = db.RecordIndicators.Single(r => r.indicatorId == id);
            return View(recordindicator);
        }

        //
        // POST: /RecordIndicator/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            RecordIndicator recordindicator = db.RecordIndicators.Single(r => r.indicatorId == id);
            db.RecordIndicators.DeleteObject(recordindicator);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}